package com.wx.domain.service.impl;

import com.wx.domain.convert.SubjectInfoConverter;
import com.wx.domain.entity.SubjectInfoBO;
import com.wx.domain.handler.subject.SubjectTypeHandler;
import com.wx.domain.handler.subject.SubjectTypeHandlerFactory;
import com.wx.domain.service.SubjectInfoDomainService;
import com.wx.subject.common.entity.PageResult;
import com.wx.subject.common.enums.IsDeletedFlagEnum;
import com.wx.subject.infra.basic.entity.SubjectInfo;
import com.wx.subject.infra.basic.entity.SubjectLabel;
import com.wx.subject.infra.basic.entity.SubjectMapping;
import com.wx.subject.infra.basic.service.SubjectInfoService;
import com.wx.subject.infra.basic.service.SubjectLabelService;
import com.wx.subject.infra.basic.service.SubjectMappingService;
import com.wx.subject.infra.basic.utils.MyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: xx-club
 * @BelongsPackage: com.wx.domain.service.impl
 * @Author: wangx
 * @CreateTime: 2024-03-14  10:56
 * @Description:
 * @Version: 1.0
 */
@Service
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
    @Resource
    private SubjectInfoService  subjectInfoService;
    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectLabelService subjectLabelService;
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        SubjectInfo subjectInfo = MyUtils.Dto2Entity(subjectInfoBO, SubjectInfo.class);
        //设置题目未删除
        subjectInfo.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        //将数据插入题目信息表
        subjectInfoService.insert(subjectInfo);
        //将数据插入相应题目类型表
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getSubjectTypeHandler(subjectInfoBO.getSubjectType());
        subjectInfoBO.setId(subjectInfo.getId());
        //拿到相应的题目类型策略处理器，执行相应的操作
        handler.add(subjectInfoBO);
        //将数据插入题目映射表，一个题目有多个分类和标签
        List<Integer> labelIds = subjectInfoBO.getLabelIds();
        List<Integer> categoryIds = subjectInfoBO.getCategoryIds();
        List<SubjectMapping> mappingList = new LinkedList<>();
        //将数据批量插入题目分类关系映射表
        labelIds.forEach(labelId ->{
            categoryIds.forEach(categoryId ->{
                SubjectMapping subjectMapping = new SubjectMapping();
                //设置题目id
                subjectMapping.setSubjectId(subjectInfo.getId());
                //设置分类id
                subjectMapping.setCategoryId(Long.valueOf(categoryId));
                //设置标签id
                subjectMapping.setLabelId(Long.valueOf(labelId));
                //设置未删除
                subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                mappingList.add(subjectMapping);
            });
        });
        //批量插入
        subjectMappingService.insertBatch(mappingList);
    }

    @Override
    public PageResult<SubjectInfoBO> getPage(SubjectInfoBO subjectInfoBO) {
        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBO.getPageNo());
        pageResult.setPageSize(subjectInfoBO.getPageSize());
        int start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();
        SubjectInfo subjectInfo = MyUtils.Dto2Entity(subjectInfoBO, SubjectInfo.class);
        int count = subjectInfoService.countByCondition(subjectInfo, subjectInfoBO.getCategoryId(), subjectInfoBO.getLabelId());
        if (count == 0) {
            return pageResult;
        }
        List<SubjectInfo> subjectInfoList = subjectInfoService.queryPage(subjectInfo, subjectInfoBO.getCategoryId()
                , subjectInfoBO.getLabelId(), start, subjectInfoBO.getPageSize());
        List<SubjectInfoBO> subjectInfoBOS = SubjectInfoConverter.INSTANCE.convertListInfoToBO(subjectInfoList);
        subjectInfoBOS.forEach(info -> {
            SubjectMapping subjectMapping = new SubjectMapping();
            subjectMapping.setSubjectId(info.getId());
            List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
            List<Long> labelIds = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
            List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIds);
            List<String> labelNames = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
            info.setLabelName(labelNames);
        });
        pageResult.setRecords(subjectInfoBOS);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public SubjectInfoBO getSubjectInfo(SubjectInfoBO subjectInfoBO) {
        return null;
    }
}


